package com.feng.system.controller;

import com.feng.common.SystemConstants;
import com.feng.core.Result;
import com.feng.system.domain.SysDept;
import com.feng.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门信息
 *
 * @author RoyWang
 */
@RestController
@RequestMapping("/system/dept")
public class SysDeptController {

    @Autowired
    private ISysDeptService deptService;

    /**
     * 获取部门列表
     */
    @GetMapping("/list")
    public Result list(SysDept dept) {
        List<SysDept> deptList = deptService.selectDeptList(dept);
        return Result.success(deptList);
    }

    /**
     * 新增部门
     */
    @PostMapping("/add")
    public Result add(@RequestBody SysDept dept) {
        if (SystemConstants.NOT_UNIQUE.equals(deptService.checkDeptNameUnique(dept))) {
            return Result.error("部门名称已存在, 新增失败");
        }
        return Result.request(deptService.insertDept(dept));
    }

    /**
     * 根据部门ID获取信息
     */
    @GetMapping(value = "/getDeptById/{deptId}")
    public Result getInfo(@PathVariable Integer deptId) {
        return Result.success(deptService.selectDeptById(deptId));
    }

    /**
     * 修改部门
     */
    @PostMapping("/update")
    public Result updateDept(@RequestBody SysDept dept) {
        return Result.request(deptService.updateDept(dept));
    }

    /**
     * 删除部门
     */
    @DeleteMapping("{deptId}")
    public Result remove(@PathVariable Integer deptId) {
        return Result.request(deptService.deleteDeptById(deptId));
    }

}
